home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FishMarket 1.0
/
FishMarket v1.0.iso
/
fishies
/
526-550
/
disk_548
/
labelmaker
/
labelmakerv1.5.lzh
/
LabelLoad.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-22
|
23KB
|
784 lines
#include <exec/types.h>
#include <intuition/intuition.h>
#include <graphics/gfx.h>
#include <iff2.h>
#include <libraries/dos.h>
#include <exec/memory.h>
#include "LabelDef.h"
extern LONG PicWidth, PicHeight, PicLeftEdge, PicTopEdge, PicOn;
extern UWORD MyColorMap[16];
extern struct LabelText *FirstText;
extern struct RastPort *prp;
BYTE DecomBuffer[128];
UWORD WorkMap[32];
/*----------------------------------------------------------------------*
* unpacker.c Convert data from "cmpByteRun1" run compression. 11/15/85
*
* By Jerry Morrison and Steve Shaw, Electronic Arts.
* This software is in the public domain.
*
* control bytes:
* [0..127] : followed by n+1 bytes of data.
* [-1..-127] : followed by byte to be repeated (-n)+1 times.
* -128 : NOOP.
*
* This version for the Commodore-Amiga computer.
*----------------------------------------------------------------------*/
/*----------- UnPackRow ------------------------------------------------*/
#define UGetByte() (*source++)
#define UPutByte(c) (*dest++ = (c))
/* Given POINTERS to POINTER variables, unpacks one row, updating the source
* and destination pointers until it produces dstBytes bytes. */
BOOL UnPackRow(pSource, pDest, srcBytes0, dstBytes0)
BYTE **pSource, **pDest; WORD srcBytes0, dstBytes0; {
register BYTE *source = *pSource;
register BYTE *dest = *pDest;
register WORD n;
register BYTE c;
register WORD srcBytes = srcBytes0, dstBytes = dstBytes0;
BOOL error = TRUE; /* assume error until we make it through the loop */
WORD minus128 = -128; /* get the compiler to generate a CMP.W */
while( dstBytes > 0 ) {
if ( (srcBytes -= 1) < 0 ) goto ErrorExit;
n = UGetByte();
if (n >= 0) {
n += 1;
if ( (srcBytes -= n) < 0 ) goto ErrorExit;
if ( (dstBytes -= n) < 0 ) goto ErrorExit;
do { UPutByte(UGetByte()); } while (--n > 0);
}
else if (n != minus128) {
n = -n + 1;
if ( (srcBytes -= 1) < 0 ) goto ErrorExit;
if ( (dstBytes -= n) < 0 ) goto ErrorExit;
c = UGetByte();
do { UPutByte(c); } while (--n > 0);
}
}
error = FALSE; /* success! */
ErrorExit:
*pSource = source; *pDest = dest;
return(error);
}
/*----------------------------------------------------------------------*
* packer.c Convert data to "cmpByteRun1" run compression. 11/15/85
*
* By Jerry Morrison and Steve Shaw, Electronic Arts.
* This software is in the public domain.
*
* control bytes:
* [0..127] : followed by n+1 bytes of data.
* [-1..-127] : followed by byte to be repeated (-n)+1 times.
* -128 : NOOP.
*
* This version for the Commodore-Amiga computer.
*----------------------------------------------------------------------*/
#define DUMP 0
#define RUN 1
#define MinRun 3
#define MaxRun 128
#define MaxDat 128
LONG putSize;
#define GetByte() (*source++)
#define PutByte(c) { *dest++ = (c); ++putSize; }
char buf[256]; /* [TBD] should be 128? on stack?*/
BYTE *PutDump(dest, nn) BYTE *dest; int nn; {
int i;
PutByte(nn-1);
for(i = 0; i < nn; i++) PutByte(buf[i]);
return(dest);
}
BYTE *PutRun(dest, nn, cc) BYTE *dest; int nn, cc; {
PutByte(-(nn-1));
PutByte(cc);
return(dest);
}
#define OutDump(nn) dest = PutDump(dest, nn)
#define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
/*----------- PackRow --------------------------------------------------*/
/* Given POINTERS TO POINTERS, packs one row, updating the source and
destination pointers. RETURNs count of packed bytes.*/
LONG PackRow(pSource, pDest, rowSize)
BYTE **pSource, **pDest; LONG rowSize; {
BYTE *source, *dest;
char c,lastc = '\0';
BOOL mode = DUMP;
short nbuf = 0; /* number of chars in buffer */
short rstart = 0; /* buffer index current run starts */
source = *pSource;
dest = *pDest;
putSize = 0;
buf[0] = lastc = c = GetByte(); /* so have valid lastc */
nbuf = 1; rowSize--; /* since one byte eaten.*/
for (; rowSize; --rowSize) {
buf[nbuf++] = c = GetByte();
switch (mode) {
case DUMP:
/* If the buffer is full, write the length byte,
then the data */
if (nbuf>MaxDat) {
OutDump(nbuf-1);
buf[0] = c;
nbuf = 1; rstart = 0;
break;
}
if (c == lastc) {
if (nbuf-rstart >= MinRun) {
if (rstart > 0) OutDump(rstart);
mode = RUN;
}
else if (rstart == 0)
mode = RUN; /* no dump in progress,
so can't lose by making these 2 a run.*/
}
else rstart = nbuf-1; /* first of run */
break;
case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
/* output run */
OutRun(nbuf-1-rstart,lastc);
buf[0] = c;
nbuf = 1; rstart = 0;
mode = DUMP;
}
break;
}
lastc = c;
}
switch (mode) {
case DUMP: OutDump(nbuf); break;
case RUN: OutRun(nbuf-rstart,lastc); break;
}
*pSource = source;
*pDest = dest;
return(putSize);
}
/*-------------------------------------------------------------------*/
CheckChunkName( ChunkName)
LONG ChunkName;
{
int a, b, ok;
a = 0;
ok = 1;
while (( a < 4)&&(ok == 1))
{
b = (ChunkName >> (a*8)) & 255;
ok = 0;
if (((b > 64)&&(b < 91)) || ((b > 47)&&(b < 58)))
{
ok = 1;
}
a++;
}
return( ok);
}
LoadCMAP( fh, The_Map, Length)
struct FileHandle *fh;
UWORD *The_Map;
LONG Length;
{
int i;
UBYTE ColBuf;
for (i=0; i < Length; i++)
{
The_Map[i] = 0;
Read( fh, &ColBuf, 1L);
The_Map[i] = ColBuf << 4;
Read( fh, &ColBuf, 1L);
The_Map[i] = The_Map[i] | ColBuf;
Read( fh, &ColBuf, 1L);
The_Map[i] = The_Map[i] | (ColBuf >> 4);
}
}
gatherchunks( DataFile, BitMapHeader,
CodedData, DataLength,
CodedColor, ColorLength)
struct FileHandle *DataFile;
struct BMHDchunk *BitMapHeader;
BYTE **CodedData;
LONG *DataLength;
UWORD *CodedColor;
LONG *ColorLength;
{
ULONG ChunkLen = 0, accomplish = 0;
ULONG Buffer;
UWORD BufferWord;
UBYTE ColBuf, IDBuffer[5];
int fehler, i;
int a, b;
fehler =0;
IDBuffer[4] =0;
while ((accomplish !=0xffff)&&(fehler < 10))
{
Read( DataFile, &Buffer, 4L);
Read( DataFile, &ChunkLen, 4L);
switch(Buffer)
{
case FORM: Read( DataFile, &Buffer, 4L);
if (Buffer != ILBM)
{
accomplish = 0xffff;
fehler = 12;
}
else
{
accomplish = accomplish | 0xf;
}
break;
case BMHD: Read( DataFile, BitMapHeader, ChunkLen);
accomplish = accomplish | 0xf0;
break;
case CAMG: Read( DataFile, &Buffer, 4L);
break;
case CMAP: *ColorLength = ChunkLen/3;
LoadCMAP( DataFile, CodedColor, *ColorLength);
accomplish = accomplish | 0xf00;
break;
case BODY: accomplish = accomplish | 0xf000;
*DataLength = ChunkLen;
if ((*CodedData = AllocMem( ChunkLen, MEMF_PUBLIC))== NULL)
{
accomplish = 0xffff;
fehler = 13;
}
else
{
Read( DataFile, *CodedData, ChunkLen);
}
break;
case GRAB: for (i=0; i < ChunkLen; i++)
{
Read( DataFile, &ColBuf, 1L);
}
break;
case CCRT: for (i=0; i < ChunkLen; i++)
{
Read( DataFile, &ColBuf, 1L);
}
break;
default: strncpy( IDBuffer, &Buffer, 4);
if ( CheckChunkName( Buffer) == 1)
{
for (i=0; i < ChunkLen; i++)
{
Read( DataFile, &ColBuf, 1L);
}
fehler++;
if (fehler == 10) accomplish = 0xffff;
}
else
{
accomplish = 0xffff;
fehler = 14;
}
break;
}
}
if (fehler < 10 )
{
fehler = 0;
}
return( fehler);
}
DecodeILBM( OnRP, BitMapHeader, CodedMap, MapLength)
struct RastPort *OnRP;
struct BMHDchunk *BitMapHeader;
BYTE *CodedMap;
LONG MapLength;
{
struct BitMap *MyBitMap;
BYTE *DecomHelp, *InCodeNOW, *BitMapPos;
PLANEPTR ActualPlanes[8]; /* 8 Planes forever ? */
LONG i, Zeile, Lage, RectHeight, RectWidth;
WORD ScreenWIP, ImageWIP;
WORD ScreenWIB, ImageWIB;
WORD ScreenHIL, ImageHIL;
WORD ScreenDIP, ImageDIP;
WORD DisplayWIB, DisplayHIL, MissTries;
WORD Still2Go;
MyBitMap = OnRP->BitMap;
ScreenWIB = MyBitMap->BytesPerRow;
ScreenHIL = MyBitMap->Rows;
ScreenDIP = MyBitMap->Depth;
ScreenWIP = ScreenWIB * 8;
ImageHIL = BitMapHeader->Height;
ImageDIP = BitMapHeader->Depth;
ImageWIP = BitMapHeader->Width;
ImageWIB = ImageWIP/16;
if ( (ImageWIB*16) < ImageWIP ) ImageWIB++;
ImageWIB *=2;
RectWidth = ( ImageWIP > ScreenWIP)? ScreenWIP: ImageWIP;
DisplayHIL = ( ImageHIL > ScreenHIL)? ScreenHIL: ImageHIL;
DisplayWIB = ( ImageWIB > ScreenWIB)? ScreenWIB: ImageWIB;
RectHeight = DisplayHIL;
SetAPen( OnRP, 0L);
RectFill( OnRP, 0L, 0L, RectWidth -1, RectHeight -1);
if ((BitMapHeader->Masking) == 1) ImageDIP++; /* A Mask ! */
PicWidth = RectWidth;
PicHeight = RectHeight;
InCodeNOW = CodedMap;
MissTries = 0;
Still2Go = 32000; /* Sorry, EA made me do it ( the WORD param) */
for (i=0; i < ScreenDIP; i++)
{
ActualPlanes[i] = MyBitMap->Planes[i];
}
for (Zeile=0; Zeile < DisplayHIL; Zeile++)
{
for (Lage=0; Lage < ImageDIP; Lage++)
{
if (((*BitMapHeader).Compression) == 1)
{
DecomHelp = DecomBuffer;
if ( UnPackRow(&InCodeNOW, &DecomHelp, Still2Go, ImageWIB) == TRUE)
{
MissTries++;
}
}
else
{
CopyMem( InCodeNOW, &DecomBuffer[0],(LONG)ImageWIB);
InCodeNOW += ImageWIB;
Still2Go -= ImageWIB;
}
if (Lage < ScreenDIP)
{
BitMapPos = ActualPlanes[Lage] + Zeile*ScreenWIB;
CopyMem( &DecomBuffer[0], BitMapPos,(LONG)DisplayWIB);
}
}
}
return (MissTries);
}
LoadILBM( ImageFile, WhichRP)
struct FileHandle *ImageFile;
struct RastPort *WhichRP;
{
struct BMHDchunk MyBMHD;
BYTE *ImageMemory;
int bruch, back;
LONG Colors, CodeLen;
LONG The_Colors;
ImageMemory = NULL;
back = 1;
The_Colors = 1;
if ((bruch = gatherchunks( ImageFile, &MyBMHD, &ImageMemory, &CodeLen,
&WorkMap[0], &The_Colors))==0)
{
bruch = DecodeILBM( WhichRP, &MyBMHD, ImageMemory, CodeLen);
if ( bruch == 0)
{
PicOn = 1;
if ( The_Colors > 16) The_Colors = 16;
CopyMem( &WorkMap[0], &MyColorMap[0], The_Colors * 2);
back = 0;
}
else
{
LMMessage("Picture not corretly saved.");
PicOn = 0;
}
}
else
{
LMMessage("Not a correct IFF-ILBM file.");
}
if (ImageMemory != NULL) FreeMem( ImageMemory, CodeLen);
return( back);
}
int
LoadPicture( WhichRP, FileToActOn)
struct RastPort *WhichRP;
char *FileToActOn;
{
struct FileHandle *ImageFile;
int bruch;
ImageFile = NULL;
if (ImageFile = (struct FileHandle *) Open( FileToActOn, MODE_OLDFILE))
{
PointerState( 2);
bruch = LoadILBM( ImageFile, WhichRP);
Close( ImageFile);
PointerState( 0);
}
else
{
LMMessage("Unable to open that file.");
bruch = 1;
}
return( bruch);
}
struct LabelText
*LoadLabel( LabelName)
BYTE *LabelName;
{
struct FileHandle *fh;
struct LabelText *Back, *Help;
int ok, t;
LONG LongBuffer;
BYTE IDBuffer[5];
LONG NumOfTexts, fault, Colors;
Back = NULL;
fh = NULL;
fault = 1;
if (fh = (struct FileHandle *) Open( LabelName, MODE_OLDFILE))
{
if ((ok = Read( fh, IDBuffer, 4L)) == 4)
{
if (strncmp( IDBuffer, "FORM", 4) == 0)
{
if ((ok = Read( fh, &LongBuffer, 4L)) == 4)
{
if ((ok = Read( fh, IDBuffer, 4L)) == 4)
{
if (strncmp( IDBuffer, "LABL", 4) == 0)
{
if ((ok = Read( fh, IDBuffer, 4L)) == 4)
{
if (strncmp( IDBuffer, "LBLH", 4) == 0)
{
ok = Read( fh, &LongBuffer, 4L);
ok = Read( fh, &PicOn, 4L);
ok = Read( fh, &PicLeftEdge, 4L);
ok = Read( fh, &PicTopEdge, 4L);
ok = Read( fh, &NumOfTexts, 4L);
if ( ok == 4)
{
DeleteAllTexts( FindStart( FirstText));
FirstText = NULL;
if ( PicOn == 1)
{
ok = LoadILBM( fh, prp);
}
else
{
ok = Read( fh, IDBuffer, 4L);
ok = Read( fh, &LongBuffer, 4L);
Colors = LongBuffer /3;
if (strncmp( IDBuffer, "CMAP",4) ==0)
{
LoadCMAP( fh, WorkMap, Colors);
CopyMem( WorkMap, MyColorMap, Colors *2);
}
}
t =0;
fault =0;
while (( t < NumOfTexts)&&(fault == 0))
{
Back = LoadText( fh);
if ( Back != NULL)
{
Back->AttrNumber = FindAttrNo(
Back->FontName,
Back->FontSize);
if (FirstText == NULL)
{
FirstText = Back;
}
else
{
if ( Back != FirstText)
{
Help = AddText( FirstText, Back);
if ( Help == NULL) fault =1;
}
}
}
else
{
fault = 1;
}
t++;
}
if ((Back == NULL)&&(FirstText != NULL))
{
DeleteAllTexts( FindStart( FirstText));
FirstText = NULL;
}
}
}
}
}
}
}
}
}
if (fault != 0) LMMessage("Read Error on that file.");
Close( fh);
}
else
{
LMMessage("Unable to open that file.");
}
return( Back);
}
int
SaveCMAP( fh, The_Map, Length, WrState)
struct FileHandle *fh;
UWORD *The_Map;
LONG Length;
int WrState;
{
LONG LongBuffer;
BYTE ByteBuffer;
int a;
LongBuffer = CMAP;
WrState = LMWrite( fh, &LongBuffer, 4L, WrState);
LongBuffer = 3* Length;
WrState = LMWrite( fh, &LongBuffer, 4L, WrState);
for (a=0; a < Length; a++)
{
ByteBuffer = (UBYTE) ((The_Map[a] >> 4)& 0xf0);
WrState = LMWrite( fh, &ByteBuffer, 1L, WrState);
ByteBuffer = (UBYTE) (The_Map[a] & 0xf0);
WrState = LMWrite( fh, &ByteBuffer, 1L, WrState);
ByteBuffer = (UBYTE) ((The_Map[a] << 4)& 0xf0);
WrState = LMWrite( fh, &ByteBuffer, 1L, WrState);
}
return( WrState);
}
int
SaveLabel( LabelName)
char *LabelName;
{
struct FileHandle *OutFile;
struct BitMap *MyMap;
ULONG LongBuffer, VBuffer, CompactLength;
USHORT WordBuffer;
BYTE ByteBuffer;
BYTE *RawRaster, *DecomHelp;
PLANEPTR mpl[8]; /* I said it before */
int t, a, b;
WORD ImageDIP, ImageWIB, ImageWIP, ImageHIL;
WORD ScreenWIB, ScreenDIP, ScreenHIL, ScreenWIP;
WORD Xaspect, Yaspect;
LONG Ebene, Zeile, nColors, ItsAPad, WrSt;
LONG PictureLength, FileLength;
LONG NumberOfTexts;
struct LabelText *Curr, *Back;
BYTE IDBuffer[5];
PointerState( 2);
MyMap = prp->BitMap;
nColors = 1 << (MyMap->Depth);
if ( PicOn == 1)
{
ScreenWIB = MyMap->BytesPerRow;
ScreenHIL = MyMap->Rows;
ScreenDIP = MyMap->Depth;
ScreenWIP = ScreenWIB * 8;
ImageHIL = PicHeight;
ImageWIP = PicWidth;
ImageDIP = MyMap->Depth;
ImageWIB = (( ImageWIP % 16 ) == 0)? ImageWIP / 16: (ImageWIP/16) +1;
ImageWIB *= 2;
for(a = 0; a < ImageDIP; a++)
mpl[a] = MyMap->Planes[a];
CompactLength = 0;
for (Zeile = 0; Zeile < ImageHIL; Zeile++)
for (Ebene = 0; Ebene < ImageDIP; Ebene++)
{
RawRaster = mpl[Ebene]+ScreenWIB*Zeile;
DecomHelp = DecomBuffer;
CompactLength += PackRow( &RawRaster, &DecomHelp, ImageWIB);
}
if ((CompactLength & 1) != 0)
{
CompactLength++;
ItsAPad = 1;
}
else
{
ItsAPad = 0;
}
PictureLength = 8 + 4 +28 +(8+ 3*nColors) +8 +CompactLength;
}
else
{
PictureLength = 8 + 3*nColors;
}
Curr = FindStart( FirstText);
NumberOfTexts =0;
while ( Curr != NULL)
{
Curr = Curr->NextText;
NumberOfTexts++;
}
FileLength = 4 +8 +16 +PictureLength +
NumberOfTexts*(sizeof( struct LabelText) -4);
if (OutFile = (struct FileHandle *) Open( LabelName, MODE_NEWFILE))
{
strcpy( IDBuffer, "FORM");
WrSt = LMWrite( OutFile, &IDBuffer, 4, 1);
LongBuffer = FileLength;
WrSt = LMWrite( OutFile, &LongBuffer, 4, WrSt);
strcpy( IDBuffer, "LABL");
WrSt = LMWrite( OutFile, &IDBuffer, 4, WrSt);
strcpy( IDBuffer, "LBLH");
WrSt = LMWrite( OutFile, &IDBuffer, 4, WrSt);
LongBuffer = 16;
WrSt = LMWrite( OutFile, &LongBuffer, 4, WrSt);
LongBuffer = (PicOn == 1) ? 1: 0;
WrSt = LMWrite( OutFile, &LongBuffer, 4, WrSt);
LongBuffer = PicLeftEdge;
WrSt = LMWrite( OutFile, &LongBuffer, 4, WrSt);
LongBuffer = PicTopEdge;
WrSt = LMWrite( OutFile, &LongBuffer, 4, WrSt);
LongBuffer = NumberOfTexts;
WrSt = LMWrite( OutFile, &LongBuffer, 4, WrSt);
if ( PicOn == 1)
{
LongBuffer = FORM;
WrSt = LMWrite( OutFile, &LongBuffer, 4L, WrSt);
LongBuffer = 4 +28 +(8+ 3*nColors) +8 +CompactLength;
WrSt = LMWrite( OutFile, &LongBuffer, 4L, WrSt);
LongBuffer = ILBM;
WrSt = LMWrite( OutFile, &LongBuffer, 4L, WrSt);
LongBuffer = BMHD;
WrSt = LMWrite( OutFile, &LongBuffer, 4L, WrSt);
LongBuffer = 20;
WrSt = LMWrite( OutFile, &LongBuffer, 4L, WrSt);
WrSt = LMWrite( OutFile, &ImageWIP, 2L, WrSt);
WrSt = LMWrite( OutFile, &ImageHIL, 2L, WrSt);
LongBuffer = 0;
WrSt = LMWrite( OutFile, &LongBuffer, 4L, WrSt);
ByteBuffer = (UBYTE) ImageDIP;
WrSt = LMWrite( OutFile, &ByteBuffer, 1L, WrSt);
ByteBuffer = 2;
WrSt = LMWrite( OutFile, &ByteBuffer, 1L, WrSt);
ByteBuffer = 1;
WrSt = LMWrite( OutFile, &ByteBuffer, 1L, WrSt);
ByteBuffer = 0;
WrSt = LMWrite( OutFile, &ByteBuffer, 1L, WrSt);
WrSt = LMWrite( OutFile, &LongBuffer, 2L, WrSt);
Xaspect = 10;
Yaspect = 22;
WordBuffer = (Xaspect << 8) | Yaspect;;
WrSt = LMWrite( OutFile, &WordBuffer, 2L, WrSt);
WordBuffer = ScreenWIP;
WrSt = LMWrite( OutFile, &WordBuffer, 2L, WrSt);
WordBuffer = ScreenHIL;
WrSt = LMWrite( OutFile, &WordBuffer, 2L, WrSt);
WrSt = SaveCMAP( OutFile, MyColorMap, nColors, WrSt);
LongBuffer = BODY;
WrSt = LMWrite( OutFile, &LongBuffer, 4L, WrSt);
WrSt = LMWrite( OutFile, &CompactLength, 4L, WrSt);
for (Zeile = 0; Zeile < ImageHIL; Zeile++)
for (Ebene = 0; Ebene < ImageDIP; Ebene++)
{
RawRaster = mpl[Ebene]+ScreenWIB*Zeile;
DecomHelp = DecomBuffer;
CompactLength = PackRow( &RawRaster, &DecomHelp, ImageWIB);
WrSt = LMWrite( OutFile, DecomBuffer, CompactLength, WrSt);
}
if (ItsAPad == 1)
{
ByteBuffer = -128;
WrSt = LMWrite( OutFile, &ByteBuffer, 1L, WrSt);
}
}
else
{
WrSt = SaveCMAP( OutFile, MyColorMap, nColors, WrSt);
}
Curr = FindStart( FirstText);
t =0;
while( Curr != NULL)
{
t++;
WrSt = SaveText( OutFile, Curr);
Curr = Curr->NextText;
}
if (WrSt == -1) LMMessage("Write error on that file");
Close( OutFile);
}
else
{
LMMessage("Unable to open that file");
}
PointerState( 0);
return( WrSt);
}
int
LMWrite( The_File, The_Data, The_Length, FormerState)
struct FileHandle *The_File;
BYTE *The_Data;
ULONG The_Length;
int FormerState;
{
if (FormerState != -1)
{
FormerState = Write( The_File, The_Data, The_Length);
}
return( FormerState);
}